<!doctype html>
<html>
<head>
 <meta charset="utf-8">
 <title>粘贴吸顶</title>
 <style>
  * {
   margin: 0;
   padding: 0;
  }
  #nav {
   width: 100%;
   height: 50px;
   background: skyblue;
  }
 </style>
 <script>
  window.onload = function() {
   var oDiv = document.getElementById('nav')
   var divT = oDiv.offsetTop
   window.onscroll = function() {
    // 获取当前页面的滚动条纵坐标位置 （依次为火狐谷歌、safari、IE678）
    var scrollT = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop
    if (scrollT >= divT) {
        // 兼容IE6代码
        // window.navigator.userAgent.indexOf('MSIE 6.0') != -1
        // oDiv.style.position = 'absolute';
        // oDiv.style.top = scrollT + 'px';
        // oDiv.style.left = 0 + 'px';
       oDiv.style.position = 'fixed'
       oDiv.style.top = 0
       oDiv.style.left = 0
     } else {
       oDiv.style.position = ''
     }
   }
  }
 </script>
</head>
<body>
 <div>
  以上<br>
  以上<br>
  以上<br>
  以上<br>
  以上<br>
  以上<br>
  以上<br>
  以上<br>
  以上<br>
  以上<br>
  <div id="nav"></div>
  啦啦啦啦啦111<br>
  啦啦啦啦啦111<br>
  啦啦啦啦啦111<br>
  啦啦啦啦啦111<br>
  啦啦啦啦啦111<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦33333<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦<br>
  啦啦啦啦啦999<br>
  啦啦啦啦啦999<br>
  啦啦啦啦啦999<br>
  啦啦啦啦啦999<br>
 </div>
</body>
</html>